<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css" integrity="sha256-2H3fkXt6FEmrReK448mDVGKb3WW2ZZw35gI7vqHOE4Y=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Gemini","version":"8.7.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":15},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>
<meta name="description" content="本文参考CSDN ID：椰子牛 的【前端面试题-可拖拽元素的实现】进行分析及润色 所用属性mouseEvent.clientX, mouseEvent.clientY         提供事件发生时的应用客户端区域的水平坐标   mouseEvent.layerX, mouseEvent.layerX 在当前触发鼠标事件的元素和它的祖先元素中找到最近的具有定位属性的元素，计算鼠标与其的偏移值，以找">
<meta property="og:type" content="article">
<meta property="og:title" content="元素拖拽与触碰边界判定">
<meta property="og:url" content="http://example.com/%E5%85%83%E7%B4%A0%E6%8B%96%E6%8B%BD%E4%B8%8E%E8%A7%A6%E7%A2%B0%E8%BE%B9%E7%95%8C%E5%88%A4%E5%AE%9A/index.html">
<meta property="og:site_name" content="受靈夢的博客">
<meta property="og:description" content="本文参考CSDN ID：椰子牛 的【前端面试题-可拖拽元素的实现】进行分析及润色 所用属性mouseEvent.clientX, mouseEvent.clientY         提供事件发生时的应用客户端区域的水平坐标   mouseEvent.layerX, mouseEvent.layerX 在当前触发鼠标事件的元素和它的祖先元素中找到最近的具有定位属性的元素，计算鼠标与其的偏移值，以找">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://typora555.oss-cn-shanghai.aliyuncs.com/typora555/%E5%85%83%E7%B4%A0%E5%AE%BD%E9%AB%98.png">
<meta property="og:image" content="https://typora555.oss-cn-shanghai.aliyuncs.com/typora555/%E7%82%B9.png">
<meta property="article:published_time" content="2021-09-15T11:29:18.000Z">
<meta property="article:modified_time" content="2021-09-16T01:02:14.359Z">
<meta property="article:author" content="受靈夢">
<meta property="article:tag" content="HTML">
<meta property="article:tag" content="JS">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://typora555.oss-cn-shanghai.aliyuncs.com/typora555/%E5%85%83%E7%B4%A0%E5%AE%BD%E9%AB%98.png">


<link rel="canonical" href="http://example.com/%E5%85%83%E7%B4%A0%E6%8B%96%E6%8B%BD%E4%B8%8E%E8%A7%A6%E7%A2%B0%E8%BE%B9%E7%95%8C%E5%88%A4%E5%AE%9A/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://example.com/%E5%85%83%E7%B4%A0%E6%8B%96%E6%8B%BD%E4%B8%8E%E8%A7%A6%E7%A2%B0%E8%BE%B9%E7%95%8C%E5%88%A4%E5%AE%9A/","path":"元素拖拽与触碰边界判定/","title":"元素拖拽与触碰边界判定"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>元素拖拽与触碰边界判定 | 受靈夢的博客</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">受靈夢的博客</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">悟道之旅</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">5</span></a></li>
        <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">0</span></a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">4</span></a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%89%80%E7%94%A8%E5%B1%9E%E6%80%A7"><span class="nav-number">1.</span> <span class="nav-text">所用属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B"><span class="nav-number">2.</span> <span class="nav-text">代码示例</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-overview">
            <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="受靈夢"
      src="/images/youkuli.png">
  <p class="site-author-name" itemprop="name">受靈夢</p>
  <div class="site-description" itemprop="description">本博客用于记录前端学习者的学习路程，悟道不可避免！！！</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">4</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">5</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



          </div>
        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/%E5%85%83%E7%B4%A0%E6%8B%96%E6%8B%BD%E4%B8%8E%E8%A7%A6%E7%A2%B0%E8%BE%B9%E7%95%8C%E5%88%A4%E5%AE%9A/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/youkuli.png">
      <meta itemprop="name" content="受靈夢">
      <meta itemprop="description" content="本博客用于记录前端学习者的学习路程，悟道不可避免！！！">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="受靈夢的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          元素拖拽与触碰边界判定
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-09-15 19:29:18" itemprop="dateCreated datePublished" datetime="2021-09-15T19:29:18+08:00">2021-09-15</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-09-16 09:02:14" itemprop="dateModified" datetime="2021-09-16T09:02:14+08:00">2021-09-16</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p>本文参考CSDN ID：椰子牛 的【前端面试题-可拖拽元素的实现】进行分析及润色</p>
<h3 id="所用属性"><a href="#所用属性" class="headerlink" title="所用属性"></a>所用属性</h3><p><strong>mouseEvent.clientX, mouseEvent.clientY</strong>        </p>
<p>提供事件发生时的应用客户端区域的水平坐标</p>
<br>

<p><strong>mouseEvent.layerX, mouseEvent.layerX</strong></p>
<p>在当前触发鼠标事件的元素和它的祖先元素中找到最近的具有定位属性的元素，计算鼠标与其的偏移值，以找到元素的 border的左上角的外交点作为相对点。若没找到则相对于页面计算</p>
<br>

<p><strong>element.clientX, element.clientY</strong></p>
<p> 属性表示元素的内部宽度，以像素计。该属性包括内边距 padding，但不包括边框 border、外边距 margin 和垂直滚动条（如果有的话）</p>
<br>

<p><strong>元素宽度示意图</strong></p>
<span id="more"></span>

<br>

<p><img src="https://typora555.oss-cn-shanghai.aliyuncs.com/typora555/%E5%85%83%E7%B4%A0%E5%AE%BD%E9%AB%98.png" alt="元素宽高"></p>
<p><strong>各点位置示意图</strong></p>
<p>item为需要移动的元素</p>
<p><img src="https://typora555.oss-cn-shanghai.aliyuncs.com/typora555/%E7%82%B9.png" alt="点"></p>
<h3 id="代码示例"><a href="#代码示例" class="headerlink" title="代码示例"></a>代码示例</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">&quot;X-UA-Compatible&quot;</span> <span class="attr">content</span>=<span class="string">&quot;IE=edge&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">title</span>&gt;</span>元素拖拽<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="css"></span></span><br><span class="line"><span class="css">    <span class="selector-id">#item</span> &#123;</span></span><br><span class="line"><span class="css">      <span class="comment">/* 通过改变定位移动元素 */</span></span></span><br><span class="line"><span class="css">      <span class="attribute">position</span>: absolute;</span></span><br><span class="line"><span class="css">      <span class="attribute">width</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="css">      <span class="attribute">height</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="css">      <span class="attribute">background-color</span>: black;</span></span><br><span class="line"><span class="css">    &#125;</span></span><br><span class="line"><span class="css">  </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;item&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 文档根元素的宽高，也就是页面宽高</span></span></span><br><span class="line"><span class="javascript">    <span class="keyword">let</span> windowHeigth, windowWidth;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 元素宽高</span></span></span><br><span class="line"><span class="javascript">    <span class="keyword">let</span> elementHeigth, elementWidth;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 元素的坐标</span></span></span><br><span class="line"><span class="javascript">    <span class="keyword">let</span> elementX, elementY;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 鼠标点击相对于元素的坐标</span></span></span><br><span class="line"><span class="javascript">    <span class="keyword">let</span> clickX, clickY;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 鼠标位置 - 元素内部的鼠标相对位置 = 元素拖动时的左上角的相对坐标</span></span></span><br><span class="line"><span class="javascript">    <span class="keyword">let</span> moveX, moveY;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 移动开始的标志</span></span></span><br><span class="line"><span class="javascript">    <span class="keyword">let</span> flag = <span class="literal">false</span>;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 获取被点击的元素</span></span></span><br><span class="line"><span class="javascript">    <span class="keyword">let</span> item = <span class="built_in">document</span>.querySelector(<span class="string">&#x27;#item&#x27;</span>);</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 鼠标按下回调</span></span></span><br><span class="line"><span class="javascript">    item.addEventListener(<span class="string">&#x27;mousedown&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">      windowWidth = <span class="built_in">document</span>.documentElement.clientWidth</span></span><br><span class="line"><span class="javascript">      windowHeigth = <span class="built_in">document</span>.documentElement.clientHeight;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">      elementWidth = item.clientWidth;</span></span><br><span class="line"><span class="javascript">      elementHeigth = item.clientHeight;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">      <span class="comment">// 鼠标按下时的位置</span></span></span><br><span class="line"><span class="javascript">      clickX = event.layerX;</span></span><br><span class="line"><span class="javascript">      clickY = event.layerY;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">      flag = <span class="literal">true</span>;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    &#125;)</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 鼠标移动回调</span></span></span><br><span class="line"><span class="javascript">    item.addEventListener(<span class="string">&#x27;mousemove&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">      item.style.cursor = <span class="string">&#x27;grab&#x27;</span>;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">      moveX = event.clientX - clickX;</span></span><br><span class="line"><span class="javascript">      moveY = event.clientY - clickY;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">      <span class="keyword">if</span> (flag) &#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 元素跟着鼠标移动</span></span></span><br><span class="line"><span class="javascript">        item.style.left = <span class="string">`<span class="subst">$&#123;moveX&#125;</span>px`</span>;</span></span><br><span class="line"><span class="javascript">        item.style.top = <span class="string">`<span class="subst">$&#123;moveY&#125;</span>px`</span>;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">        <span class="comment">// 边缘停止判定</span></span></span><br><span class="line"><span class="javascript">        <span class="keyword">if</span> (moveX &lt; <span class="number">0</span>) &#123;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">          flag = <span class="literal">false</span>;</span></span><br><span class="line"><span class="javascript">        &#125;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">if</span> (moveY &lt; <span class="number">0</span>) &#123;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">          flag = <span class="literal">false</span>;</span></span><br><span class="line"><span class="javascript">        &#125;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">if</span> (moveX + elementWidth &gt; windowWidth) &#123;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">          flag = <span class="literal">false</span>;</span></span><br><span class="line"><span class="javascript">        &#125;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">if</span> (moveY + elementHeigth &gt; windowHeigth) &#123;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">          flag = <span class="literal">false</span>;</span></span><br><span class="line"><span class="javascript">        &#125;</span></span><br><span class="line"><span class="javascript">      &#125;</span></span><br><span class="line"><span class="javascript">    &#125;)</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 鼠标抬起时回调</span></span></span><br><span class="line"><span class="javascript">    item.addEventListener(<span class="string">&#x27;mouseup&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">      flag = <span class="literal">false</span>;</span></span><br><span class="line"><span class="javascript">    &#125;)</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">  </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>


    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/HTML/" rel="tag"># HTML</a>
              <a href="/tags/JS/" rel="tag"># JS</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BD%BF%E7%94%A8WebSockets/" rel="prev" title="客户端使用 WebSockets">
                  <i class="fa fa-chevron-left"></i> 客户端使用 WebSockets
                </a>
            </div>
            <div class="post-nav-item">
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">受靈夢</span>
</div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  
<script src="/js/third-party/search/local-search.js"></script>




  





</body>
</html>
